package edu.guet.trafficforecastsystem.service.impl;

import edu.guet.trafficforecastsystem.mapper.UserMapper;
import edu.guet.trafficforecastsystem.model.User;
import edu.guet.trafficforecastsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * Created by Caesar on 2017/5/22.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private PlatformTransactionManager transactionManager;


    @Override
    public User queryUserByName(String name) {
        return userMapper.selectByName(name);
    }

/*    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public int addUserUsingTransactional(User user) {
        User target = queryById(id);
        Preconditions.checkArgument(target != null, "Invalid id %d", id);

        int rowsAffected = userMapper.addUser(user);

        if (shouldRollback()) {
            System.out.println("Manually rolling back changes!!!");
            // Could also trigger rollback by throwing exceptions, see rollbackFor
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }

        System.out.println("Committing changes...");
        return rowsAffected;
    }*/

    private boolean shouldRollback() {
        return false;
    }
}
